from flask import render_template, current_app, jsonify, session
from . import news_blu
from info333.models import User
from info333.utils.response_code import RET


@news_blu.route('/')
def index():
    # 5.首页右上角登录状态显示
    # 1.尝试从缓存中获取用户信息
    user_id = session.get('user_id')
    print(user_id)
    user = None
    if user_id:
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DATAERR, errmsg='用户查询异常')
    data = {
        'user_info': user.to_dict() if user else None
    }
    print('测试登录状态')
    print(data)
    return render_template('news/index.html', data=data)


@news_blu.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('news/favicon.ico')
